home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d945.lha / Reminder / src / src.lha / ARexx.c next >
C/C++ Source or Header  |  1993-04-19  |  4KB  |  144 lines

  1. /* ARexx handling routines for ReminderCheck */
  2.  
  3. /* $Id: ARexx.c,v 1.2 1993/04/18 22:14:28 Matti_Rintala Exp $ */
  4.  
  5. #include <string.h>
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <rexx/storage.h>
  10. #include <rexx/rxslib.h>
  11.  
  12. #ifdef __SASC
  13. #include <proto/exec.h>
  14. #include <proto/rexxsyslib.h>
  15. #endif
  16.  
  17. #include "Constants.h"
  18. #include "ARexx.h"
  19.  
  20. /* Calculate maximum length of the command line */
  21. /* 'address "PORT" "COMMAND"' or rexxfilename "datestr" "textstr" */
  22. #define MAXCMDLINE 10+AREXXPORTLEN+3+AREXXCOMLEN+2+TEXTLEN
  23.  
  24. /* Buffer for ARexx command */
  25. static char cmdline[MAXCMDLINE+1];
  26.  
  27. /* Port for ARexx replys */
  28. static struct MsgPort *MyPort = NULL;
  29.  
  30. struct Library *RexxSysBase = NULL;
  31.  
  32. /* initarexx initializes ARexx resources */
  33. void initarexx(void) {
  34.  
  35.   /* Open RexxSys library */
  36.   RexxSysBase = OpenLibrary(RXSNAME, 0);
  37.  
  38.   /* Open port */
  39.   MyPort = CreateMsgPort();
  40. }
  41.  
  42. /* releasearexx releases ARexx resources */
  43. void releasearexx(void) {
  44.  
  45.   /* Close RexxSys library */
  46.   if (RexxSysBase != NULL) {
  47.     CloseLibrary(RexxSysBase);
  48.     RexxSysBase = NULL;
  49.   }
  50.  
  51.   /* Close port */
  52.   if (MyPort != NULL) {
  53.     DeleteMsgPort(MyPort);
  54.     MyPort = NULL;
  55.   }
  56. }
  57.  
  58. /* makearexx handles the ARexx part of reminding */
  59. void makearexx(struct EventNode *node, time_t *date) {
  60.   
  61.   struct RexxMsg *msg;
  62.   char *buffer = cmdline;        /* Pointer to command line */
  63.   int len;
  64.   struct MsgPort *aport;
  65.  
  66.   /* Return if No Arexx mode event or if no message port or library */
  67.   if (((node->mode & ~GROUPEDMASK) != AREXXCMODE &&
  68.        (node->mode & ~GROUPEDMASK) != AREXXSMODE) ||
  69.       MyPort == NULL || RexxSysBase == NULL)
  70.     return;
  71.  
  72.   if ((msg = CreateRexxMsg(MyPort, NULL, NULL)) != NULL) {
  73.     /* Now calculate correct argument string to buffer */
  74.     if ((node->mode & ~GROUPEDMASK) == AREXXCMODE) {
  75.       /* For ARexx command, argument is 'address "PORT" "COMMAND"' */
  76.       strcpy(buffer, "\'address \"");
  77.       buffer += 10;
  78.  
  79.       strncpy(buffer, node->arexxport, AREXXPORTLEN);
  80.       len = strlen(node->arexxport);
  81.       buffer += (len > AREXXPORTLEN) ? AREXXPORTLEN : len;
  82.  
  83.       *buffer++ = '\"';
  84.       *buffer++ = ' ';
  85.       *buffer++ = '\"';
  86.  
  87.       strncpy(buffer, node->arexxcom, AREXXCOMLEN);
  88.       len = strlen(node->arexxcom);
  89.       buffer += (len > AREXXCOMLEN) ? AREXXCOMLEN : len;
  90.  
  91.       *buffer++ = '\"';
  92.       *buffer++ = '\'';
  93.     }
  94.     else {
  95.       /* Script command is format scriptfile "datestr" "textstr" */
  96.       struct tm *d;        /* For calculating datestr */
  97.  
  98.       strncpy(buffer, node->arexxcom, AREXXCOMLEN);
  99.       len = strlen(node->arexxcom);
  100.       buffer += (len > AREXXCOMLEN) ? AREXXCOMLEN : len;
  101.  
  102.       *buffer++ = ' ';
  103.       *buffer++ = '\"';
  104.  
  105.       d = localtime(date);    /* Convert date to correct format */
  106.       buffer += strftime(buffer, 16, "%a %d-%b-%Y", d);    /* Print datestr */
  107.  
  108.       *buffer++ = '\"';
  109.       *buffer++ = ' ';
  110.       *buffer++ = '\"';
  111.  
  112.       strncpy(buffer, node->text, TEXTLEN);
  113.       len = strlen(node->text);
  114.       buffer += (len > TEXTLEN) ? TEXTLEN : len;
  115.  
  116.       *buffer++ = '\"';
  117.     }
  118.  
  119.     /* Calculate length of argument string */
  120.     len = (int)(buffer - cmdline);
  121.     /* Make ARexx argument string */
  122.     if ((msg->rm_Args[0] = CreateArgstring(cmdline, len)) != NULL) {
  123.       msg->rm_Action = RXCOMM | RXFF_NOIO; /* ARexx command flags */
  124.  
  125.       /* Then find AREXX port and send message */
  126.       Forbid();
  127.       if ((aport = FindPort("AREXX")) != NULL)
  128.     PutMsg(aport, (struct Message *)msg);
  129.       Permit();
  130.  
  131.       /* If sending was succesfull, wait for reply and remove it */
  132.       if (aport) {
  133.     WaitPort(MyPort);
  134.     GetMsg(MyPort);
  135.       }
  136.  
  137.       ClearRexxMsg(msg, 1);    /* Clear message */
  138.     }
  139.     DeleteRexxMsg(msg);        /* Delete message */
  140.   }
  141. }
  142.  
  143.  
  144.